<template>
  <div class="book_edit_list">
    <div class="header_top">
      <el-button type="primary"  @click="handleClick(1)">发布</el-button>
      <br>
      <br>
      <el-form :inline="true" :model="form" ref="form">
        <el-form-item>
          <el-input v-model="form.title" placeholder="搜索它不香吗"></el-input>
        </el-form-item>
        <el-form-item><el-button @click="searchClick">搜索</el-button></el-form-item>
      </el-form>
    </div>
    <div class="main">
      <el-table
        :data="tableData"
        :border="true"
        v-loading = "loading"
        @selection-change="handelSelectTable"
      >
        <el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
        <el-table-column type="selection" align="center"></el-table-column>
        <el-table-column label="标题" prop="title"></el-table-column>
        <el-table-column label="发布人" prop="realName" width="120"></el-table-column>
        <el-table-column label="操作">
          <template>
            <el-button type="success" @click="handleClick(2)">编辑</el-button>
            <el-button type="danger" @click="handleClick(3)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <br>
      <el-pagination
        background
        layout="total , prev, pager, next, jumper"
        @current-change="current_change"
        :total="total">
      </el-pagination>

    </div>

    <el-dialog title="编辑" :visible.sync="dialogFormVisible" width="70%">
      <el-form  ref="formEdit" :rules="rulesEdit" :model="formEdit" label-width="120px">
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="formEdit.title" ></el-input>
        </el-form-item>
        <el-form-item label="文章类型" prop="type">
          <el-select v-model="formEdit.type" placeholder="选择文章类型">
            <el-option label="java" value="1"></el-option>
            <el-option label="vue" value="2"></el-option>
            <el-option label="android" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="文章内容" prop="content">
          <editor-bar v-model="formEdit.content" :isClear="isClear" @change="change"></editor-bar>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleClick(4)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import EditorBar from '@/components/wangEndui'
  import qs from 'qs'
  export default {
    name: 'BookEditList',
    components: { EditorBar },
    data: function () {
      return {
        loading: false,
        pageNum: 1,
        limit: 10,
        total: 0,
        selectTable: [],
        form:{
          title: ''
        },
        tableData: [
          {"title": "yyyyy","editUsername": "admin"}
        ],
        dialogFormVisible: false,
        formEdit: {
          title: "",
          content: "",
          type: "",
        },
        lable_width: "120",
        isClear: false,
        rulesEdit: {
          title: {required:true , message: '标题不能为空' ,trigger: 'blur'},
          type: {required:true , message: '类别未选择' ,trigger: 'blur'},
          content: {required:true , message: '内容不能为空' ,trigger: 'blur'},
        }
      }
    },
    methods:{
      getData(){
        let _this = this
        let map = {}
        map.page = this.pageNum
        map.limit = this.limit

        if (this.form.title !== ''){
          map.title = this.form.title
        }
        _this.loading = true
        this.$api.getBookList(map).then(res=>{
          _this.total = res.data.total
          _this.tableData = res.data.list
          _this.loading = false
        })
      },
      searchClick () {
        this.getData()
      },
      handleClick (type) {
        switch (type) {
          case 1:
            this.dialogFormVisible = true
            break
          case 2:
            if (this.selectTable.length > 1){
              this.$message.warning("只能选择一行进行编辑")
            }else if (this.selectTable.length === 0 ){
              this.$message.warning("请选择一个文章进行编辑")
            }else{

            }
            break
          case 3:
            if (this.selectTable.length > 1){
              this.$message.success("删除当前页全选")
            }else if (this.selectTable.length === 0 ){
              this.$message.warning("请选择一个文章进行删除")
            }else{

            }
            break
          case 4:
            let _this  = this
            this.$refs['formEdit'].validate((valid) => {
              if (valid) {
                _this.$api.addBlog(_this.formEdit).then(res=>{
                  if (res.code === 1){
                    _this.$message.success("创作成功")
                  }else {
                    _this.$message.success("创作失败，请重试！")
                  }
                  _this.dialogFormVisible = false;
                  _this.getData()
                })
              } else {
                console.log('error submit!!');
                return false;
              }
            });
            break
        }
      },
      handelSelectTable (val) {
        this.selectTable = val
      },
      current_change (val) {
        this.pageNum  = val
        this.getData();
      },
      change (val) {
        this.form.content = val
      }
    },
    created(){
      this.loading = true
      this.getData()
    }
  }
</script>

<style scoped>

</style>
